package com.Cychat.post.service.impl;




import com.Cychat.commons.entity.pojo.MessageChatRecord;
import com.Cychat.commons.entity.pojo.Post;
import com.Cychat.post.entity.dto.PostDTO;
import com.Cychat.post.entity.vo.PostVO;
import com.Cychat.post.mapper.PostMapper;

import com.Cychat.post.service.FavoriteService;
import com.Cychat.post.service.PostService;
import com.Cychat.commons.config.OssConfig;
import com.aliyun.oss.OSS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: 崔航
 * @Date: 2025/10/28:14:28
 * @Description:
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {
    @Autowired
    private PostMapper postMapper;

    @Autowired
    private OSS ossClient;

    @Autowired
    private OssConfig ossConfig;
    @Autowired
    private FavoriteService favoriteService;
    @Override
    public List<PostDTO> getAllPosts(Integer pageNum, Integer pageSize) {
        Page<Post> page = new Page<>(pageNum, pageSize);
        List<Post> allPosts = postMapper.getAllPosts(page);
        return allPosts.stream()
                .map(PostDTO::fromPost)
                .collect(Collectors.toList());
    }

    @Override
    public PostDTO getPostById(Integer postId, Long userId) {
        Post postById = postMapper.getPostById(postId);
        PostDTO postDTO = PostDTO.fromPost(postById);
        postDTO.setIsFavorite(favoriteService.isFavorite(postId, userId) ? 1 : 0);
        return postDTO;
    }

    @Override
    public void addPost(PostVO postVO) {
        String[] images = postVO.getImages();
        // 上传图片到OSS并获取新的URL数组
        String imagesString = String.join(",", images);
        Post post = new Post();
        post.setUserId(postVO.getUserId());
        post.setTitle(postVO.getTitle());
        post.setContent(postVO.getContent());
        post.setImages(imagesString);
        post.setStatus(1);
        post.setLikeCount(0);
        post.setFavoriteCount(0);
        post.setCommentCount(0);
        post.setViewCount(0);
        post.setCreateTime(new Date());
        post.setUpdateTime(new Date());
        this.save(post);
    }
    @Override
    public String updateAvatar(MultipartFile file) throws IOException {
        try {
            // 指定文件夹路径，例如: avatars/用户名/文件名
            String folderPath = "post/";

            // 生成唯一文件名
            String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();

            // 完整的对象键（包含文件夹路径）
            String objectName = folderPath + fileName;

            // 上传文件
            ossClient.putObject(ossConfig.getBucketName(), objectName, file.getInputStream());

            // 返回访问URL
            return "https://" + ossConfig.getBucketName() + "." + ossConfig.getEndpoint() + "/" + objectName;
        } finally {
            // 注意：这里不再关闭ossClient，因为它现在是Spring管理的Bean
        }
    }

    @Override
    public List<PostDTO> getPostByUserId(Long userId) {
        List<Post> posts = postMapper.getPostByUserId(userId);
        return posts.stream()
                .map(PostDTO::fromPost)
                .collect(Collectors.toList());
    }

    @Override
    public PostDTO like(Integer postId) {;
        postMapper.increaseLikeCount(postId);
        return null;
    }

    @Override
    public List<PostDTO> getPost() {
        List<Post> allPosts = postMapper.getPost();
        return allPosts.stream()
                .map(PostDTO::fromPost)
                .collect(Collectors.toList());
    }

}
